package com.chang.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chang.common.utils.PageUtils;
import com.chang.common.utils.Query;
import com.chang.product.entity.BrandEntity;
import com.chang.product.entity.CategoryBrandRelationEntity;
import com.chang.product.mapper.CategoryBrandRelationDao;
import com.chang.product.service.BrandService;
import com.chang.product.service.CategoryBrandRelationService;
import com.chang.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    // 循环依赖是两个或者多个bean都需要将对方的bean在初始化之前就要注入进来,spring容器没法解决这种先有鸡还是先有蛋的问题
    // 这里的解决方法是使用@Lazy对某一个bean对象进行懒加载
    @Lazy
    @Autowired
    BrandService brandService;
    @Lazy //
    @Autowired
    CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDeatil(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        // 查询详细名字并注入
        categoryBrandRelation.setBrandName(brandService.getById(brandId).getName());
        categoryBrandRelation.setCatelogName(categoryService.getById(catelogId).getName());
        this.save(categoryBrandRelation);
    }

    /*
     * @Author Chang
     * @Date 2024/3/6 10:04
     * @Description 关联表特定属性修改
     * @Param [brandId, name]
     * @Return void
     * @Since version 1.0
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    @Override
    public void updateCategory(Long catelogId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(catelogId);
        categoryBrandRelationEntity.setCatelogName(name);
        this.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catelogId));
    }

    @Override
    public List<BrandEntity> getbrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.getBaseMapper().selectList(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId));
        List<BrandEntity> brandEntityList = new ArrayList<>();
        categoryBrandRelationEntities.forEach((item) -> {
            BrandEntity brandEntity = new BrandEntity();
            brandEntity.setBrandId(item.getBrandId());
            brandEntity.setName(item.getBrandName());
            brandEntityList.add(brandEntity);
        });
        return brandEntityList;
    }

}